package com.tedu.service.Impl;

import com.tedu.Common.enumerator.ServiceCode;
import com.tedu.Common.ex.ServiceException;
import com.tedu.dao.repository.IUserRepository;
import com.tedu.dao.repository.OrdersRepository;
import com.tedu.pojo.entity.Orders;
import com.tedu.pojo.entity.User;
import com.tedu.pojo.param.BookParam;
import com.tedu.pojo.vo.HotelRoomVo;
import com.tedu.pojo.vo.HotelVo;
import com.tedu.pojo.vo.UserStandardVO;
import com.tedu.service.IBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Service
@Repository
@Slf4j
public class BookServiceImpl implements IBookService {

    @Autowired
    private OrdersRepository ordersRepository;
    @Autowired
    private IUserRepository userRepository;

    @Override
    public Long addNew(BookParam bookParam) {
        log.debug("开始处理【预定】的业务，参数：{}", bookParam);

        /*判断酒店是否存在*/
        log.debug("查询酒店是否存在，用户选择的酒店ID:" + bookParam.getHotelId());
        HotelVo hotelVo = ordersRepository.selectByHotelId(bookParam.getHotelId());
        if (hotelVo == null && bookParam.getHotelId() == 0L) {
            String message = "所选择的酒店不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        /*判断所选酒店的房间是否存在*/
        HotelRoomVo hotelRoomVo = ordersRepository.selectByCategoryId(bookParam.getHotelId(), bookParam.getCategoryId());
        if (hotelRoomVo == null) {
            String message = "该酒店无所选的房间类型";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        /*判断用户信息*/
        User user = new User();
        UserStandardVO standardById = userRepository.getStandardById(bookParam.getUserId());
        if (standardById == null){
            String message = "用户不能为空";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if (user.getUsername() == null || user.getPhone() == null || user.getId_code() == null){
            String message = "用户身份信息不完整";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        /*判断填写时间是否正常*/
        if (bookParam.getStartTime() == null || bookParam.getEndTime()== null){
            String message = "时间为空，请填写时间";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int result = bookParam.getStartTime().compareTo((bookParam.getEndTime()));
        if (result >= 0) {
            String message = "时间不合理";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        Orders orders = new Orders();
        try {
            /*天数*/
            Long dayNum = getDayNum(bookParam.getStartTime(), bookParam.getEndTime());
            orders.setNum(dayNum);

            /*价格*/
            HotelRoomVo hotelPrice = ordersRepository.selectByCategoryId(bookParam.getHotelId(), bookParam.getCategoryId());
            orders.setPrice(hotelPrice.getPrice() * dayNum);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return orders.getId();
    }

    public Long getDayNum(Date startTime, Date endTime) throws ParseException {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date start = dft.parse(String.valueOf(startTime));
        Date end = dft.parse(String.valueOf(endTime));
//        Date start =dft.parse(startTime);
//        Date end =dft.parse(endTime);
        long starTime = start.getTime();
        long eTime = end.getTime();
        long num = eTime - starTime;
        return num / 24 / 60 / 60 / 1000;
    }
}
